import javax.crypto.spec.PSource;
import java.util.Random;
import java.util.Scanner;

public class TestDemo {
    public static void main1(String[] args) {
        //    1. 根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入年龄");
            int age = scanner.nextInt();
            if (age <= 18) {
                System.out.println("少年");
            } else if (age <= 28 && age >= 19) {
                System.out.println("青年");
            } else if (age <= 55 && age >= 29) {
                System.out.println("中年");
            } else {
                System.out.println("老年");
            }
        }
    }

    public static void main2(String[] args) {
//        2. 判定一个数字是否是素数
        Scanner scanner = new Scanner(System.in);
        while (true) {
            int count = 0;
            System.out.println("请输入一个数字");
            int num = scanner.nextInt();
            for (int i = 2; i <= num - 1; i++) {
                if (num % i == 0) {
                    count++;
                }
            }
            if (count == 0) {
                System.out.println(num + "是素数");
            } else {
                System.out.println(num + "不是素数");
            }
        }
    }

    public static void main3(String[] args) {
//        3. 打印 1 - 100 之间所有的素数
        for (int i = 1; i <= 100; i++) {
            int count = 0;
            for (int j = 2; j <= i - 1; j++) {
                if (i % j == 0) {
                    count++;
                }
            }
            if (count == 0) {
                System.out.println(i);
            }
        }
    }

    public static void main4(String[] args) {
//        4. 输出 1000 - 2000 之间所有的闰年
        for (int i = 1000; i <= 2000; i++) {
            if (i % 4 == 0) {
                if (i % 100 != 0) {
                    System.out.println(i);
                } else {
                    if (i % 400 == 0) {
                        System.out.println(i);
                    }
                }
            }
        }
    }

    public static void main5(String[] args) {
//        5. 输出乘法口诀表
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf(" " + i + "*" + j + "=" + (i * j));
            }
            System.out.println();
        }
    }

    public static void main6(String[] args) {
//        求两个正整数的最大公约数
//        辗转相除法
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个数");
        int s1 = scanner.nextInt();
        System.out.println("请输入第二个数");
        int s2 = scanner.nextInt();
        if (s1 >= s2) {
            int a = s1 % s2;
            while (a != 0) {
                s1 = s2;
                s2 = a;
                a = s1 % s2;
            }
            System.out.println("最大公约数为" + s2);
        } else {
            int b = s2 % s1;
            while (b != 0) {
                s2 = s1;
                s1 = b;
                b = s2 % s1;
            }
            System.out.println("最大公约数为" + s1);
        }
    }

    public static void main7(String[] args) {
//        7. 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
        double sum = 0;
        double ret = 0;
        for (int i = 1; i <= 100; i++) {
            sum += 1 / i;
            i += 2;
        }
        for (int i = 2; i <= 100; i++) {
            ret += 1 / i;
            i += 2;
        }
        System.out.println(sum - ret);
    }

    public static void main8(String[] args) {
//        编写程序数一下 1到 100 的所有整数中出现多少个数字9
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 9) {
                count++;
            }
            if (i / 10 == 9) {
                count++;
            }
        }
        System.out.println("9的个数为" + count);
    }

    public static void main9(String[] args) {
//         求出0～999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数，其各位数字的立方和确好等于该数本
//身，如；153＝1＋5＋3?，则153是一个“水仙花数”。)
        for (int i = 100; i < 1000; i++) {
            int a = i / 100;
            int b = i % 100 / 10;
            int c = i % 10;
            if (a * a * a + b * b * b + c * c * c == i) {
                System.out.println(i);
            }
        }
    }

    public static void main10(String[] args) {
/*        编写代码模拟三次密码输入的场景。 最多能输入三次密码，密码正确，提示“登录成功”,
        密码错误可以重新输入，最多输入三次。三次均错，则提示退出程序*/
        Random random = new Random();
        int count = 0;
        Scanner scanner = new Scanner(System.in);
        int guess = random.nextInt(50);
        for (int i = 3; i > 0; i--) {
            System.out.println("你还有" + i + "次输入密码的机会");
            System.out.println("请输入》");
            int num = scanner.nextInt();
            if (num == guess) {
                System.out.println("密码正确，登陆成功。");
            } else if (num > guess) {
                System.out.println("大了");
                System.out.println("密码错误");
                count++;
            } else if (num < guess) {
                System.out.println("小了");
                System.out.println("密码错误");
                count++;
            }
        }
        if (count == 3) {
            System.out.println("退出程序");
        }
    }

    public static void main11(String[] args) {
//            完成猜数字游戏
        Random random = new Random();
        int guess = random.nextInt(100);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入猜的数字(1-100)");
            int num = scanner.nextInt();
            if (num > guess) {
                System.out.println("大了");
            } else if (num < guess) {
                System.out.println("小了");
            } else {
                System.out.println("猜对了");
                break;
            }
        }
    }
}
